//
//  SimpleNSThreadVC.m
//  thread
//
//  Created by 关旭 on 2019/3/8.
//  Copyright © 2019 guanxu. All rights reserved.
//

#import "SimpleNSThreadVC.h"

@interface SimpleNSThreadVC ()
{
    NSThread *oneThread;
    UIImageView *showImage;
    UILabel *imageState;
}

@end

@implementation SimpleNSThreadVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    
//    [self createThread];//创建简单线程例子
    
    //使用多线程下载图片例子
    [self initView];
    
    [self loadImage];
}
#pragma mark -----------------NSThread 下载图片的简单例子
- (void)initView {
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 200, 200)];
    imageView.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:imageView];
    imageView.center = self.view.center;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 200, 40)];
    label.textColor = [UIColor blackColor];
    label.backgroundColor = [UIColor clearColor];
    [self.view addSubview:label];
    label.center = self.view.center;
    label.text = @"请稍候";
    label.textAlignment = NSTextAlignmentCenter;
    
    
    showImage = imageView;
    imageState = label;
}
- (void)loadImage {
    NSThread *thread = [[NSThread alloc]initWithBlock:^{
        UIImage *image = [[UIImage alloc]initWithData:[NSData dataWithContentsOfURL:[NSURL URLWithString:@"http://i1.bvimg.com/679654/2017809566581a8b.jpg"]]];//该图片地址只有七天有效期
        [NSThread sleepForTimeInterval:2];
//        图片下载完成之后使用主线程来执行更新UI的操作
        [self performSelectorOnMainThread:@selector(updateImage:) withObject:image waitUntilDone:NO];
    }];
    [thread start];
}
- (void)updateImage:(UIImage *)image {
    [imageState removeFromSuperview];
    imageState = nil;
    showImage.image = image;
}

#pragma mark ----------------NSThread 的简单创建
- (void)createThread {
    //创建一个线程，线程任务执行体为 ooneThread：方法，接收参数是 @"data"
    NSThread *thread = [[NSThread alloc]initWithTarget:self selector:@selector(oneThread:) object:@"data"];
    thread.name = @"oneThread";//设置线程的名字
    [thread start];
    oneThread = thread;
    
//    block 方式创建线程
//    NSThread *thread = [[NSThread alloc]initWithBlock:^{
//        for (int i=0; i<5; i++) {
//            NSLog(@"task %@",[NSThread currentThread]);
//        }
//    }];
//    [thread setName:@"oneThread"];
//    [thread start];
    
    
//    类方法创建并自动启动一个线程
//    [NSThread detachNewThreadWithBlock:^{
//        for (int i=0; i<5; i++) {
//            NSLog(@"task %@",[NSThread currentThread]);
//        }
//
//    }];
    
    
//    类方法创建并自启动一个线程，并传入一个参数
//    [NSThread detachNewThreadSelector:@selector(oneThread:) toTarget:self withObject:@"data"];
    
}
- (void)oneThread:(NSString *)obj {
    for (int i=0; i<5; i++) {
        if (i == 3) {
             [NSThread exit];
        }
        NSLog(@"Task %@  %@",[NSThread currentThread],obj);
    }
    NSLog(@"oneThread complete");
   
}
/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
